package com.qfedu.lc.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.param.UserVipAdd;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.entity.Vip;
import com.qfedu.lc.user.entity.VipOrder;
import com.qfedu.lc.user.mapper.VipMapper;
import com.qfedu.lc.user.mapper.VipOrderMapper;
import com.qfedu.lc.user.service.VipOrderService;
import com.qfedu.lc.user.service.VipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.lc.user.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 8.用户收费会员表 服务实现类
 * </p>
 *
 * @author wgz
 * @since 2024-01-29
 */
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip> implements VipService {


    @Autowired
    private VipOrderMapper vipOrderMapper;

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private WalletService walletService;

    @Autowired
    private VipOrderService vipOrderService;

    @Override
    public R buyVipUseWallet(UserVipAdd userVipAdd) {

        // 1.创建订单 未支付状态

        // 判断vip订单类型 续费 开通   查询该用户是否存在vip  存在续费   不存在开通
        int vipOrderTye = 0;

        QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();
        vipQueryWrapper.eq("user_id",userVipAdd.getUserId());
        Vip vip = vipMapper.selectOne(vipQueryWrapper);
        if (vip == null){
            vipOrderTye = 1;// 开通vip
        }else {
            vipOrderTye = 2;// vip 续费
        }

        // 订单号
        String no = "v" + System.currentTimeMillis()+ RandomUtil.randomNumbers(6);

        VipOrder vipOrder = new VipOrder(userVipAdd.getUserId(),vipOrderTye,userVipAdd.getVipType(),userVipAdd.getPayType(),userVipAdd.getMoney()*100,no,1,new Date());
        int insertOrderNum = vipOrderMapper.insert(vipOrder);
        if (insertOrderNum<=0){
            return R.fail("创建订单失败");
        }


        // 2.削减账户余额   记录余额日志
        if (!walletService.subWallet(userVipAdd.getUserId(),userVipAdd.getMoney())){
            return R.fail("扣减账户余额失败 ,余额不足");
        }

        // 3.修改订单状态已支付
       if ( !vipOrderService.updateVipOrderState(no,2)){
           return R.fail("创建订单失败");
       }

        // 4.增加  更新会员
        if (!createOrUpdateVip(userVipAdd)){
            return R.fail("vip会员续费 创建失败");
        }
        return R.ok("vip会员续费 成功");
    }

    @Override // 增加  更新会员
    public boolean createOrUpdateVip(UserVipAdd userVipAdd) {

        QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();
        vipQueryWrapper.eq("user_id",userVipAdd.getUserId());
        Vip vip = vipMapper.selectOne(vipQueryWrapper);

        if (vip ==null){ // 增加vip 会员

            Calendar calendar =Calendar.getInstance(); //当前是时间
            calendar.add(Calendar.DAY_OF_MONTH,365);
            Date eDate = calendar.getTime()  ;  // 得到一年后的今天

            vip = new Vip(userVipAdd.getUserId(),userVipAdd.getVipType(),new Date(),eDate,365,"初次创建vip会员，会员类型："+userVipAdd.getVipType(),new Date());
            int num = vipMapper.insert(vip);
            if (num>0){
                return true;
            }
        }else {  //  vip 会员续费
                // 分两种情况    1.原有vip 已经过期，需要重新开启vip 会员
                //             2.已有vip  进行续费

             if (vip.getEdate().getTime()<new Date().getTime()){  //1.原有vip 已经过期，需要重新开启vip 会员

                 vip.setSdate(new Date());
                 Calendar calendar =Calendar.getInstance(); //当前是时间
                 calendar.add(Calendar.DAY_OF_MONTH,365);
                 Date eDate = calendar.getTime()  ;  // 得到一年后的今天
                 vip.setEdate(eDate);

                 vip.setDays(365);
                 vip.setType(userVipAdd.getVipType());
                 vip.setInfo("vip过期后，重新开始续费，会员类型："+userVipAdd.getVipType());

             }else {  //2.已有vip  进行续费

                 // 开始时间不需要动
                 Calendar calendar =Calendar.getInstance(); //设置为原有会卡的结束时间再加 365
                 calendar.setTime(vip.getEdate());
                 calendar.add(Calendar.DAY_OF_MONTH,365);
                 Date eDate = calendar.getTime()  ;  // 得到 在加一年后的今天

                 vip.setEdate(eDate);
                 vip.setDays(vip.getDays()  + 365);
                 vip.setType(userVipAdd.getVipType());
                 vip.setInfo("vip 直接续费，会员类型："+userVipAdd.getVipType());

             }

            int num = vipMapper.updateById(vip);
             if (num>0){
                 return true;
             }

        }
        return false;
    }


}
